<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="model.database.DBException"%>
<%@page import="view.WebCommons"%>
<%@page import="controller.CommonValues"%>
<%@page import="controller.ManagerController"%>
<%@page import="java.util.List" %>
<%@page import="java.util.Map" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customers by expenses</title>
</head>
<body>

	<span class="customHeader2">Customers by expenses</span>

	<div id="page">
		<div class="post">
		
			<div id="sidebar">
				<ul>
					<li>
						<h2>What is minimal expense that your are looking for?</h2>
					</li>
				</ul>
			</div>

			<!-- 	get lower bound for expenses -->			
			<div id="content">
				<form METHOD=POST ACTION="GetCustomersByExpenses.jsp">
					<table>
						<tr>
							<td><INPUT TYPE=TEXT NAME=sum></td>
							<td><INPUT TYPE=SUBMIT VALUE="Go!" class="mySubmitButton"></td>
						</tr>
					</table>
				</form>
			</div>

	<%
	// check if already submitted data
	if (!(request.getParameter("sum") == null || request.getParameter("sum").equals(""))) {
		
		boolean canSearchCustomers = true; // indicates if can search customers by given parameter
		
		// parse input to numeric data
		double sum = 0;
		try {		
			sum = Double.parseDouble(request.getParameter("sum"));			
		} catch (NumberFormatException e) {
			
			// data is not numeric, ask again
			canSearchCustomers = false;
			%>
			<div id="sidebar">
				<span class="notify">Please enter a number...</span>
			</div>
			<%
		}
		
		// if can, search the customers
		if (canSearchCustomers) {
		
			try {
					
				// extract controller from session		
				ManagerController controller = (ManagerController) session.getAttribute(WebCommons.CONTROLLER);
				
				// try getting details of all customers
				List<Map<String,String>> customersDetails = controller.getCustomers(sum);
				%>
				<div id="sidebar" style="width: auto;">
					<table border=2>
						<%
						// column names
						%>
						<tr>
							<td><span class="customMenuOption">Number</span></td>
							<td><span class="customMenuOption">Name</span></td>
							<td><span class="customMenuOption">Username</span></td>
							<td><span class="customMenuOption">Address</span></td>
							<td><span class="customMenuOption">Credit Card</span></td>
						</tr>
						<%
						// display data
						int numOfCustomers = customersDetails.size();
						for (int i = 0; i < numOfCustomers; i++) {
				
							// display details of current customer
							Map<String, String> currCustomerDetails = customersDetails.get(i);				
							%>
							<tr>
								<td style="text-align: center;"><span class="customFormValue"><%=currCustomerDetails.get(CommonValues.NUMBER)%></span></td>
								<td><span class="customFormValue"><%=currCustomerDetails.get(CommonValues.NAME)%></span></td>
								<td><span class="customFormValue"><%=currCustomerDetails.get(CommonValues.USER_NAME)%></span></td>
								<td><span class="customFormValue"><%=currCustomerDetails.get(CommonValues.ADDRESS)%></span></td>
								<td><span class="customFormValue"><%=currCustomerDetails.get(CommonValues.PAYMENT_METHOD)%></span></td>
							</tr>
							<%
						}
						%>
					</table>
					
				</div>
				<%		
		
			} catch (DBException e) {
				
				// move to error page
				response.sendRedirect("../../commonPages/ErrorPage.jsp");
			}
		}
		
	}
	%>	
	
		</div>
	</div>
	<%
	
	// return to main menu
	String menuPage = (String) session.getAttribute(WebCommons.MAIN_MANU);
	%>
	<div id="footer">
		<span class="myLink"><A HREF="<%="../../menus/" + menuPage%>">Back
				to Menu</A></span>
	</div>

</body>
</html>